﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using StartReading.Security.Validation;

namespace StartReading.Tests.Security.Tests.Validation
{
    [TestFixture]
    class AliasValidationTests
    {
        private string Alias { get; set; }
        private TestDelegate IsValid { get; set; }

        private void IsAliasValid()
        {
            AliasValidation.Instance.IsValid( this.Alias );
        }

        [SetUp]
        public void BeforeEachTest()
        {
            this.IsValid = this.IsAliasValid;
        }

        [TearDown]
        public void AfterEachTest()
        {
            this.Alias = null;
            this.IsValid = null;
        }

        [Test]
        public void IsValid_EmptyAlias_ThrowsArgumentException()
        {
            this.Alias = String.Empty;
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_NullAlias_ThrowsArgumentException()
        {
            this.Alias = null;
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasContainsAlphabets_NoException()
        {
            this.Alias = "abcdef";
            Assert.DoesNotThrow( this.IsValid );    
        }

        [Test]
        public void IsValid_AliasContainsSpaceOnly_ThrowsArgumentException()
        {
            this.Alias = "    ";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasContainsUnderscoreOnly_ThrowsArgumentException()
        {
            this.Alias = "____";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasContainsDotsOnly_ThrowsArgumentException()
        {
            this.Alias = "....";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasLengthLessThanMinimumLength_ThrowsArgumentException()
        {
            this.Alias = "ab";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasLengthMoreThanMaximumLength_ThrowsArgumentException()
        {
            this.Alias = "abcdefghijklmnopqrstuvwxyz";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasContainsNumbersOnly_NoException()
        {
            this.Alias = "123";
            Assert.DoesNotThrow( this.IsValid );
        }

        [Test]
        public void IsValid_AliasContainsAlphabetsNumbers_NoException()
        {
            this.Alias = "a1b2c3";
            Assert.DoesNotThrow( this.IsValid );
        }

        [Test]
        public void IsValid_AliasContainsAlphabetsNumbersSeparatedBySpace_NoException()
        {
            this.Alias = "a1b 2c3 d4e";
            Assert.DoesNotThrow( this.IsValid );
        }

        [Test]
        public void IsValid_AliasContainsAlphabetsNumbersSeparatedByUnderscore_NoException()
        {
            this.Alias = "a1b_2c3_poco";
            Assert.DoesNotThrow( this.IsValid );
        }

        [Test]
        public void IsValid_AliasContainsAlphabetsNumbersSeparatedByDot_NoException()
        {
            this.Alias = "a1b.2c3.poco";
            Assert.DoesNotThrow( this.IsValid );
        }

        [Test]
        public void IsValid_AliasEndingWithSpace_ThrowsArgumentException()
        {
            this.Alias = "abc_123.def ";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasEndingWithUnderscore_ThrowsArgumentException()
        {
            this.Alias = "abc 123.def_";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasEndingWithDot_ThrowsArgumentException()
        {
            this.Alias = "123_abc 456.";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasStartsWithSpace_ThrowsArgumentException()
        {
            this.Alias = " 123 abc.456";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasStartsWithUnderscore_ThrowsArgumentException()
        {
            this.Alias = "_abc.123 efg";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }

        [Test]
        public void IsValid_AliasStartsWithDot_ThrowsArgumentException()
        {
            this.Alias = ".abc_123 efg";
            Assert.Throws( typeof( ArgumentException ), this.IsValid );
        }
    }
}
